﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DAL.Service.Impl;

namespace DAL.Service.admin.Impl
{
    public class SectionsServiceImpl : BaseServiceImpl, ISectionsService
    {
        public IList getList()
        {
            MyQuery query = new MyQuery();
            Hashtable total = new Hashtable();
            query.setQueryString("select * from cms.sections where state=1 order by seq,id");
            IList list = this.getList(query);
            return list;
        }

        public int getCount(object id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select count(*) as total from cms.sections where parentid=" + id.ToString());
            Hashtable ht = (Hashtable)this.getList(query)[0];
            return Convert.ToInt32(ht["total"]);
        }

        public IList getListByPId(int id)
        {
            MyQuery query = new MyQuery();
            Hashtable total = new Hashtable();
            query.setQueryString("select * from cms.sections where state=1 and parentid=@id order by seq,id");
            query.addParas("@id", id);
            IList list = this.getList(query);
            return list;
        }
        public IList getDetailById(int id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select * from cms.sections where id=@id and state=1");
            query.addParas("@id", id);
            IList list = this.getList(query);
            return list;
        }

        public int deleteFromSections(int id)
        {
            MyQuery query = new MyQuery();
            string strSQL = "update cms.sections set state = 0 where id =@id";
            query.addParas("@id", id);
            query.setQueryString(strSQL);
            return this.exeUpdateSql(query);
        }
        public int checkcode(string code, int id)
        {
            MyQuery query = new MyQuery();
            string sql = "select 1 where EXISTS(SELECT id from cms.sections where code=@code and id<>@id and state=1)";
            query.setQueryString(sql);
            query.addParas("@code", code);
            query.addParas("@id", id);
            return this.getList(query).Count;
        }

        public IList getSectionById(string id)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select * from cms.sections where id=@id");
            query.addParas("@id", id);
            return this.getList(query);

        }


        public IList getParentList()
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select * from cms.sections where state=1 and parentid=0 order by seq,id");
            IList list = this.getList(query);
            return list;
        }

        public IList getNameByCode(string code)
        {
            MyQuery query = new MyQuery();
            query.setQueryString("select name from  cms.sections where code=@code");
            query.addParas("@code", code);
            IList list = this.getList(query);
            return list;
        }
    }
}
